package userfav

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"userop_bff/controller"
	"userop_bff/forms"
	"userop_bff/global"
	"userop_bff/goods_proto"
	"userop_bff/model"
	"userop_bff/userop_proto"
)

// 获取收藏信息列表
func List(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.Query("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	list, err := global.UserFavSrvClient.GetFavList(context.Background(), &userop_proto.UserFavRequest{UserId: int32(userIdInt)})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	ids := make([]int32, 0)
	for _, datum := range list.Data {
		ids = append(ids, datum.GoodsId)
	}
	_, err = global.GoodsSrvClient.BatchGetGoods(context.Background(), &goods_proto.BatchGoodsIdInfo{Id: ids})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	var userFavList []model.UserFav
	for _, v := range list.Data {
		userFavList = append(userFavList, model.UserFav{
			Id:      0,
			UserId:  v.UserId,
			GoodsId: v.GoodsId,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "收藏列表获取成功",
		"data": map[string]interface{}{
			"total":       list.Total,
			"addressList": userFavList,
		},
	})
}

// 添加收藏
func Add(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	userFavForm := forms.UserFavForm{}
	if err := c.ShouldBind(&userFavForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	detail, err := global.UserFavSrvClient.AddUserFav(context.Background(), &userop_proto.UserFavRequest{
		UserId:  int32(userIdInt),
		GoodsId: userFavForm.GoodsId,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "添加收藏成功",
		"data": detail,
	})
}

// 删除收藏
func Delete(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	userFavForm := forms.UserFavForm{}
	if err := c.ShouldBind(&userFavForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	_, err := global.UserFavSrvClient.DeleteUserFav(context.Background(), &userop_proto.UserFavRequest{
		UserId:  int32(userIdInt),
		GoodsId: userFavForm.GoodsId,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "取消收藏成功",
		"data": nil,
	})
}

// 查看用户是否已经收藏某件商品
func Detail(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	userFavForm := forms.UserFavForm{}
	if err := c.ShouldBind(&userFavForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	detail, err := global.UserFavSrvClient.GetUserFavDetail(context.Background(), &userop_proto.UserFavRequest{
		UserId:  int32(userIdInt),
		GoodsId: userFavForm.GoodsId,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "查看收藏详情成功",
		"data": detail,
	})
}
